package {
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import gs.TweenLite;
	import gs.plugins.EndArrayPlugin;
	import gs.plugins.TweenPlugin;

	[SWF(frameRate=30, width=600, height=600, backgroundColor="#000000")]
	public class TweenShape extends Sprite
	{
		protected var sA:Array = [190, 165, 350, 167, 353, 205, 293, 208, 291, 336, 249, 339, 249, 208, 192, 208];
		protected var eA:Array = [204, 167, 290, 222, 291, 278, 244, 351, 176, 351, 125, 293, 130, 231, 162, 195];
		protected var tweenArray:Array;
		
		protected var save:Vector.<Point>;
		protected var leng:int;
		
		protected var traceMe:Sprite;
		protected var clearMe:Sprite;
		
		public function TweenShape()
		{
			TweenPlugin.activate([EndArrayPlugin]);
			
			
			stage.addEventListener(MouseEvent.CLICK, startTween);
		}
		
		public function startTween(e:MouseEvent):void
		{
			tweenArray = sA.concat();
			stage.removeEventListener(MouseEvent.CLICK, startTween);
			TweenLite.to(tweenArray, 1, {endArray:eA, onUpdate:updateDisplay, onComplete:onComplete});
			
//			save = new Vector.<Point>();
//			traceMe = makeButton(0x00FF00);
//			traceMe.addEventListener(MouseEvent.CLICK, onTraceClick);
//			traceMe.x = 550;
//			traceMe.y = 20;
//			this.addChild(traceMe);
//			
//			clearMe = makeButton(0x0000FF);
//			clearMe.addEventListener(MouseEvent.CLICK, onClearClick);
//			clearMe.x = 550;
//			clearMe.y = 70;
//			this.addChild(clearMe);
			
//			makeShape();
		}
		
		protected function onComplete():void
		{
			updateDisplay();
			stage.addEventListener(MouseEvent.CLICK, startTween);
			trace("READY");
		}
		
		protected function updateDisplay():void
		{
			var i:int=0;
			var g:Graphics = this.graphics;
			var end:int = tweenArray.length;
			g.clear();
			g.lineStyle(1, 0xFFFFFF, 1);
			
			g.moveTo(tweenArray[i++], tweenArray[i++]);
			for(i; i<end; ++i)
			{
				g.lineTo(tweenArray[i], tweenArray[++i]);
			}
			g.lineTo(tweenArray[0], tweenArray[1]);
		}
		
		protected function makeButton(color:uint):Sprite 
		{
			var sp:Sprite = new Sprite();
			var g:Graphics = sp.graphics;
			
			g.beginFill(color,1);
			g.drawRect(0,0,40,40);
			g.endFill();
			
			sp.buttonMode = true;
			
			return sp;
		}
		
		protected function makeShape():void
		{
//			this.addEventListener(Event.ENTER_FRAME, onEnter);
			stage.addEventListener(MouseEvent.CLICK, onClick);
			
		}
		
		protected function onClick(e:MouseEvent):void
		{
			if(e.target == stage)
			{
				var p:Point = new Point(this.mouseX, this.mouseY);
				save[leng++] = p;
				
				drawShape();
			}
			else
			{
				trace(e.target);
			}
		}
		
		protected function drawShape():void
		{
			var g:Graphics = this.graphics;
			g.clear();
			g.lineStyle(1,0xFFFFFF,1);
			g.moveTo(save[0].x, save[0].y);
			for(var i:int=1; i<leng; ++i)
			{
				g.lineTo(save[i].x, save[i].y);
			}
			g.lineTo(save[0].x, save[0].y);
			g.endFill();
		}
		
		protected function onClearClick(e:MouseEvent):void
		{
			this.graphics.clear();
			save = new Vector.<Point>();
			leng = 0;
		}
		
		protected function onTraceClick(e:MouseEvent):void
		{
			var str:String = "[";
			for(var i:int=0; i<leng; ++i)
			{
				str += save[i].x + ", ";
				str += save[i].y + ", ";
			}
			str += "]";
			trace(str);
		}
		
	}
}
